<template>
  <div class="index">
    <van-tabs v-model="active">
      <van-tab
        v-for="channel in channels"
        :key="channel.id"
        :title="channel.name"
      >
        <!-- 模拟很多内容 -->
        <!-- <div class="scroll-wrapper">
          {{ channel.name }}
          <p v-for="i in 20" :key="i">文章{{ i }}</p>
        </div> -->
        <article-list :channel="channel"></article-list>
      </van-tab>
    </van-tabs>
    <!--按钮  通过它打开频道管理的弹层 -->
    <div class="bar-btn" @click="openChannelList">
      <van-icon name="wap-nav" />
    </div>

    <!-- 弹层  https://vant-contrib.gitee.io/vant/#/zh-CN/action-sheet -->
    <!-- 频道管理 -->
    <van-action-sheet v-model="isShowChannelEdit" title="频道管理">
      <ChannelEdit
        :channels="channels"
        :curIndex="active"
        ref="channelEdit"
        @change-channel="hChangeChannel"
        @fixed-active-index="hFixedActiveIndex"
      ></ChannelEdit>
    </van-action-sheet>
  </div>
</template>

<script>
import ChannelEdit from './channelEdit'
import articleList from './articleList.vue'
import { getChannels } from '@/api/channel.js'
export default {
  name: 'home',
  components: {
    articleList,
    ChannelEdit
  },
  data () {
    return {
      active: 0,
      channels: [],
      isShowChannelEdit: false // 是否显示频道编辑弹层
    }
  },
  created () {
    this.loadChannel()
  },
  methods: {
    openChannelList () {
      this.isShowChannelEdit = true
      if (this.$refs.channelEdit) { this.$refs.channelEdit.isEditing = false }
    },
    async loadChannel () {
      const { data: { data } } = await getChannels()
      // console.log(data)
      this.channels = data.channels
    },
    // 处理子组件channelEdit中用户在我的频道上点击的动作
    // 父组件收到了子组件中我的频道被点击
    hChangeChannel (curIdx) {
      // 1. 切换频道
      this.active = curIdx
      // 2. 关闭弹层
      this.isShowChannelEdit = false
    },
    // 当频道管理组件中，删除的 频道 在当前频道 之前，
    // 要把active-1
    hFixedActiveIndex () {
      this.active--
    }
  }
}
</script>

<style lang="less" scoped>
// 频道管理的开关按钮
.bar-btn {
  position: fixed;
  right: 5px;
  top: 57px;
  display: flex;
  align-items: center;
  background-color: #fff;
  opacity: 0.8;
  z-index: 1;
  .van-icon-wap-nav {
    font-size: 20px;
  }
}
</style>
